package com.wx.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.*;

import static org.aspectj.bridge.MessageUtil.info;

@RestController
@RequestMapping
public class UpLoad {
    @PostMapping("/upLoad")
    public String upLoad(@RequestBody MultipartFile file, HttpSession session) throws Exception {
            String url = "F:\\wanxi\\gongzuo\\huanbao\\eight\\huanbao\\huanbaobefore09-17\\loadImg\\";
            String path;
        if (file==null){
             path = (String) session.getAttribute("logo");
            return path;
        }else {
            path="/loadImg/" + file.getOriginalFilename();
        }
            File file1 = new File(url);
            if (!file1.exists() && !file1.isDirectory()) {
                info("创建文件夹");
                file1.mkdirs();
            }
            InputStream inputStream = file.getInputStream();
            OutputStream outputStream = new FileOutputStream(url + file.getOriginalFilename());
            byte buffer[] = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, len);
            }
            inputStream.close();
            outputStream.close();
            path=".."+path;
            return path;
        }
}
